<template>
  <XtxDialog v-model:visible="visible" title="添加收货地址">
    <template v-slot:default>
      <div class="address-edit">
        <div class="xtx-form">
          <div class="xtx-form-item">
            <div class="label">收货人：</div>
            <div class="field">
              <input
                class="input"
                placeholder="请输入收货人"
                v-model="address.receiver"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">手机号：</div>
            <div class="field">
              <input
                class="input"
                placeholder="请输入手机号"
                v-model="address.contact"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">地区：</div>
            <div class="field">
              <XtxCity
                ref="xtxCityInstance"
                @onCityChange="onCityChange"
                placeholder="请选择所在地区"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">详细地址：</div>
            <div class="field">
              <input
                class="input"
                placeholder="请输入详细地址"
                v-model="address.address"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">邮政编码：</div>
            <div class="field">
              <input
                class="input"
                placeholder="请输入邮政编码"
                v-model="address.postalCode"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">地址标签：</div>
            <div class="field">
              <input
                class="input"
                placeholder="请输入地址标签，逗号分隔"
                v-model="address.addressTags"
              />
            </div>
          </div>
          <div class="xtx-form-item">
            <div class="label">是否设置为默认地址：</div>
            <div class="field">
              <input
                type="checkbox"
                class="checkbox"
                v-model="address.isDefault"
              />
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-slot:footer>
      <XtxButton
        @click="visible = false"
        type="gray"
        style="margin-right: 20px"
      >
        取消
      </XtxButton>
      <XtxButton @click="onSureClickHandler" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { ref } from "vue";
import { addAddress, updateAddressById } from "@/api/order";
import Message from "@/components/library/Message";
export default {
  name: "AddressEdit",
  setup(prop, { emit }) {
    // 用于控制弹框是否显示
    const visible = ref(false);
    // 用于储存用户选择的城市文字信息
    const location = ref();
    // 用于存储用户选择的城市地址
    const address = ref({
      provinceCode: "",
      cityCode: "",
      countyCode: "",
      // 收货人
      receiver: "",
      // 手机号
      contact: "",
      // 详细地址
      address: "",
      // 邮政编码
      postalCode: "",
      // 地址标签
      addressTags: "",
      // 默认选中
      isDefault: false,
    });
    // 城市选择组件实例
    const xtxCityInstance = ref();
    // 选择城市列表,保存城市信息
    const onCityChange = (data) => {
      location.value = data.location;
      // 储存城市信息provinceCode
      address.value.provinceCode = data.provinceCode;
      address.value.cityCode = data.cityCode;
      address.value.countyCode = data.countyCode;
    };
    //新增收货地址
    // 用于执行用户点击确定按钮之后的逻辑代码,
    const onSureClickHandler = async () => {
      // 处理发送添加地址的请求数据
      const target = {
        ...address.value,
        isDefault: address.value.isDefault ? 0 : 1,
      };
      // 判断是修改还是添加 (修改,address中会存在id）
      if (target.id) {
        // 修改
        try {
          await updateAddressById(target.id, target);
          // 关闭对话框
          visible.value = false;
          // 用户提示
          Message({ type: "success", text: "收货地址修改成功" });
          // 通知父组件更新收货地址
          emit("onAddressChanged", target.id);
        } catch (error) {
          Message({
            type: "error",
            text: `收货地址修改失败 ${error.response.data.message}`,
          });
        }
      } else {
        // 添加
        try {
          // 发送添加地址请求
          let data = await addAddress(target);
          // 关闭对话框
          visible.value = false;
          // 用户提示
          Message({ type: "success", text: "收货地址添加成功" });
          // 通知父组件更新收货地址
          emit("onAddressChanged", data.result.id);
        } catch (error) {
          // 收货地址添加失败
          Message({
            type: "error",
            text: `收货地址添加失败 ${error.response.data.message}`,
          });
        }
      }
    };

    return {
      visible,
      address,
      location,
      onSureClickHandler,
      onCityChange,
      xtxCityInstance,
    };
  },
};
</script>

<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
  .xtx-city {
    width: 320px;
    :deep(.select) {
      height: 50px;
      line-height: 48px;
      display: flex;
      padding: 0 10px;
      justify-content: space-between;
      .placeholder {
        color: #ccc;
      }
      i {
        color: #ccc;
        font-size: 18px;
      }
      .value {
        font-size: 14px;
      }
    }
    :deep(.option) {
      top: 49px;
    }
  }
}
.xtx-form {
  padding: 0;
  .xtx-form-item {
    width: auto;
    &:last-child {
      padding-bottom: 0;
    }
  }
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
</style>
